package com.example.springboot2022nocv.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.springboot2022nocv.entity.NocvData;
import com.example.springboot2022nocv.service.IndexService;
import com.example.springboot2022nocv.vo.DataView;
import com.example.springboot2022nocv.vo.NocvDataVo;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.awt.*;
import java.util.ArrayList;
import java.util.List;

@Controller
public class ChinaDataAdminController {

    @Autowired
    IndexService indexService;

    //跳转页面
    @RequestMapping("/toChinaManager")
    public String toChinaManager(){
        return "admin/ChinaDataManager";
    }

    /*
    * 模糊查询带有分页
    * */

    @RequestMapping("/listDataByPage")
    @ResponseBody
    public DataView listDataByPage(NocvDataVo nocvDataVo){
        //创建分页的对象
        IPage<NocvData> page = new Page<>(nocvDataVo.getPage(),nocvDataVo.getLimit());
        //创建模糊查询对象
        QueryWrapper<NocvData> queryWrapper = new QueryWrapper();
        queryWrapper.like(!(nocvDataVo.getName()==null),"name",nocvDataVo.getName());
        //疫情数据确诊最多排在上面
        queryWrapper.orderByDesc("value");
        //查询数据库
        indexService.page(page,queryWrapper);
        //5.返回数据封装
        DataView dataView = new  DataView(page.getTotal(),page.getRecords());

        return dataView;
    }

    @RequestMapping("/china/deleteById")
    @ResponseBody
    public DataView deleteById(Integer id){
        indexService.removeById(id);
        DataView dataView = new DataView();
        dataView.setCode(200);
        dataView.setMsg("删除中国地图数据成功!");
        return dataView;
    }
    @RequestMapping("/china/addOrUpdateChina")
    @ResponseBody
    public DataView addChina(NocvDataVo nocvDataVo){
        boolean save = indexService.save(nocvDataVo);
        DataView dataView = new DataView();
        if(save){

            dataView.setCode(200);
            dataView.setMsg("添加中国地图数据成功!");
            return dataView;
        }
        dataView.setCode(100);
        dataView.setMsg("添加中国地图数据失败!");
        return dataView;
    }

    /***
     * Excel的拖拽或上传
     */
    @RequestMapping("/excelImportChina")
    @ResponseBody
    public DataView excelImportChina(@RequestParam("file")MultipartFile file)throws Exception{
        DataView dataView = new DataView();
        //1.文件不能为空
        if(file.isEmpty()){
            dataView.setMsg("文件为空不能上传");
        }
        //2.POI获取EXCEL解析数据
        HSSFWorkbook wb = new HSSFWorkbook(file.getInputStream());
        HSSFSheet sheet = wb.getSheetAt(0);
        //3.定义一个程序集合接受文件中的数据
        List<NocvData> list = new ArrayList<>();
        HSSFRow row = null;
        //4.解析数据,装到集合里
        for(int i = 0;i<sheet.getPhysicalNumberOfRows();i++){
            //4.1定义实体
            NocvData nocvData = new NocvData();
            //4.2每一行的数据 放到实体类里面
            row = sheet.getRow(i);
            //4.3解析数据
            nocvData.setName(row.getCell(0).getStringCellValue());
            nocvData.setValue((int)row.getCell(1).getNumericCellValue());
            //5添加list集合
            list.add(nocvData);
        }
        //循环插入数据库
        indexService.saveBatch(list);
        dataView.setCode(200);
        dataView.setMsg("小可爱,excel已经被你插入成功了");
        return dataView;

    }
}
